package com.hqyj.user.controller;


import com.hqyj.user.dto.UserInfo;
import com.hqyj.user.mapper.UserMapper;
import com.hqyj.user.service.UserService;
import com.hqyj.utils.ResultUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-30
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/userLogin")
    public ResultUtils userLogin(UserInfo userInfo) {
        if(userInfo.getUserName() == "" || userInfo.getUserPwd() == ""){
            return new ResultUtils(500,"用户名或者密码不能为空",null);
        }
        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getUserName(),userInfo.getUserPwd());
            subject.login(token);
            subject.getSession().setAttribute("user",userInfo);
        }catch (UnknownAccountException e) {
            return  new ResultUtils(500,"用户名不存在",null);
        }catch (IncorrectCredentialsException e){
            return new ResultUtils(500,"密码错误",null);
        }
        return new ResultUtils(200,"欢迎登录",userInfo);
    }

    @PostMapping("/sendEmailCode")
    public ResultUtils sendEmailCode(String userEmail){
        return userService.sendEmailCode(userEmail);
    }

    @PostMapping("/emailLogin")
    public ResultUtils emailLogin(UserInfo userInfo){
        return userService.emailLogin(userInfo);
    }

    @PostMapping("/sendSmsCode")
    public ResultUtils sendSmsCode(String tel){
        return userService.sendSmsCode(tel);
    }

    @PostMapping("smsLogin")
    public ResultUtils smsLogin(UserInfo userInfo){
        return userService.smsLogin(userInfo);
    }

    @GetMapping("queryByName")
    public ResultUtils queryByName(String userName){
        return userService.queryByName(userName);
    }

    @PostMapping("selectUser")
    public ResultUtils selectUserByUserName(){
        return userService.selectUserByUserName();
    }

    @PostMapping("updateUser")
    public ResultUtils updateUser(UserInfo userInfo){
        return userService.updateUserByUserName(userInfo);
    }

    @GetMapping("logOut")
    public ResultUtils logOut(){
        return userService.logOut();
    }

    @PostMapping("userIndex")
    public ResultUtils userIndex(UserInfo userInfo){
        return userService.userIndex(userInfo);
    }

    @PostMapping("updateOtherUser")
    public ResultUtils updateOtherUser(UserInfo userInfo){
        return userService.updateOtherUser(userInfo);
    }

    @PostMapping("deleteUser")
    public ResultUtils deleteUser(UserInfo userInfo){
        return userService.deleteUser(userInfo);
    }

    @PostMapping("addUser")
    public ResultUtils addUser(UserInfo userInfo){
        return userService.addUser(userInfo);
    }

    @PostMapping("selectAllUser")
    public ResultUtils selectUser(){
        return userService.selectUser();
    }

    @GetMapping("/505")
    public ResultUtils error(){
        return new ResultUtils(505,"您还没有登录",null);
    }


}
